<?php
/**
 *
 * User: chenzheng
 * Date: 2023/5/20
 * Time: 12:48
 */

namespace app\api\library\promotion\getUserPromotionInfo;

use app\api\model\MallUser;

use think\facade\Db;

class V
{
    public static function run($param)
    {
        
        $userId = _checkEmptyParam($param[ 'userId' ], '用户不能为空');
        $date   = $param[ 'date' ];
        $type   = 17;
        $where  = [
            ['id', '=', $userId],
        ];
        
        $userInfo = MallUser::where($where)->find();
        $page     = $param[ 'page' ];
        $limit    = $param[ 'limit' ];
        $offset   = $limit * ($page - 1);
        if (empty($userInfo)) {
            _outPut(204, '', '用户不存在');
        }
        $where = [
            ['user_id', '=', $userId],
            ['trade_type', '=', $type],
            ['delete_time', '=', null],
        ];
        if ( !empty($date)) {
            $startTime = strtotime($date . '-1');
            $endTime   = strtotime(date($date . '-t 23:59:59'));
            $where[]   = ['create_time', '>', $startTime];
            $where[]   = ['create_time', '<', $endTime];
        }
        $total       = Db::name('mall_trade_record_log')
            ->where($where)
            ->count();
        $tradeRecord = Db::name('mall_trade_record_log')
            ->where($where)
            ->limit($offset, $limit)
            ->field('id,trade_type,amount,order_id,create_time')
            ->order(['id' => 'desc'])
            ->select()->toArray();
        $orderIds    = array_column($tradeRecord, 'order_id');
        $orders      = Db::view('mall_order', 'id,collage_id,order_price,order_no,pay_time,goods_name')
            ->view('mall_collage', 'collage_id,collage_no,start_time,end_time', 'mall_order.collage_id=mall_collage.collage_id')
            ->view('mall_user', 'name', 'mall_order.user_id=mall_user.id')
            ->where([['mall_order.id', 'in', $orderIds]])
            ->select();
        $orders      = $orders->toArray();
        $orders      = array_column($orders, null, 'id');
        foreach ($tradeRecord as $key => $value) {
            $tradeRecord[ $key ][ 'create_time' ] = $value[ 'create_time' ];
            $tradeRecord[ $key ][ 'amount' ]      = getPrice($value[ 'amount' ]);
            $tradeRecord[ $key ][ 'order_no' ]    = '';
            $tradeRecord[ $key ][ 'pay_time' ]    = '';
            $tradeRecord[ $key ][ 'goods_name' ]  = '';
            if (isset($orders[ $value[ 'order_id' ] ])) {
                $tradeRecord[ $key ][ 'order_no' ]    = $orders[ $value[ 'order_id' ] ][ 'order_no' ];
                $tradeRecord[ $key ][ 'pay_time' ]    = $orders[ $value[ 'order_id' ] ][ 'pay_time' ];
                $tradeRecord[ $key ][ 'goods_name' ]  = $orders[ $value[ 'order_id' ] ][ 'goods_name' ];
                $tradeRecord[ $key ][ 'pay_user' ]    = $orders[ $value[ 'order_id' ] ][ 'name' ];
                $tradeRecord[ $key ][ 'order_price' ] = getPrice($orders[ $value[ 'order_id' ] ][ 'order_price' ]);
            }
        }
        $page          = [
            'total'      => $total,
            'limit'      => $limit,
            'page'       => $page,
            'total_page' => ceil($total / $limit),
        ];
        $expendWhere   = $where;
        $expendWhere[] = ['trade_type', '=', '17'];
        $totalCount    = [
            'totalPromotion' => Db::name('mall_trade_record_log')->where($expendWhere)->value('SUM(amount)') / 100 ?? 0,
        ];
        $res           = [
            'total' => $totalCount,
            'page'  => $page,
            'list'  => $tradeRecord,
        ];
        _outPut(200, $res);
    }
}